<?php
namespace app\appoint\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\appoint\model\AdminCate as cate_model;
use app\appoint\model\AdminProject;
class AdminCate extends AdminRest
{

    protected $model;
    //预约服务模型
    protected $project_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model         = new cate_model();
        $this->project_model = new AdminProject();

    }
    /**
     * 预约分类列表
     */
    public function cateList(){
        $input = $this->_param;
        $data = $this->model->cateList(['uniacid'=>$this->_uniacid],$input['limit']);
        return $this->success($data);
    }

    /**
     *
     * 添加预约分类
     */

    public function cateAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $have= $this->model->cateInfo(['title'=>$input['title'],'uniacid'=>$this->_uniacid]);
        if (empty(trim($input['title']))&&trim($input['title'])!=='0')
        {
            return $this->error('标题必填');
        }
        if(!empty($have)){
             return $this->error('分类名字不能相同');
        }
        $res = $this->model->cateAdd($input);
        return $this->success($res);
    }
    /**
     *
     * 编辑预约分类
     */
    public function cateUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            if(!empty($input['title'])){
                if (empty(trim($input['title']))&&trim($input['title'])!=='0')
                {
                    return $this->error('标题必填');
                }
                $dis[] = ['title','=',$input['title']];
                $dis[] = ['uniacid','=',$this->_uniacid];
                $dis[] = ['id','<>',$input['id']];
                $have= $this->model->cateInfo($dis);
                if(!empty($have)){
                    return $this->error('分类名字不能相同');
                }
            }
            $data = $this->model->cateUpdate(['id'=>$input['id']],$input);
        }else{
            $data = $this->model->cateInfo(['id'=>$input['id']]);
        }
        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-01-09 10:07
     * @功能说明:删除预约分类
     */
    public function cateDel(){
        $input   = $this->_input;
        //预约服务查询条件
        $pro_dis = [
            'uniacid'    => $this->_uniacid,
            'classify_id'=> $input['id']
        ];
        //查询是否有预约服务
        $project = $this->project_model->proInfo($pro_dis);
        //如果该服务被使用则不能删除
        if(!empty($project)){
            $this->errorMsg('该分类已被使用，无法被删除');
        }
        //删除分类
        $res = $this->model->cateUpdate(['id'=>$input['id']],['status'=>-1]);

        return $this->success($res);
    }




}
